using Base.Filesystem

# 定义结构体来表示 cdl 对象
mutable struct CDLChannel
    DelayProfile::String
    PathDelays::Vector{Float64}
    AveragePathGains::Vector{Float64}
    AnglesAoD::Vector{Float64}
    AnglesAoA::Vector{Float64}
    AnglesZoD::Vector{Float64}
    AnglesZoA::Vector{Float64}
    HasLOSCluster::Bool
    KFactorFirstCluster::Float64
    AngleSpreads::Vector{Int}
    RayCoupling::String
    XPR::Float64
    InitialPhases::String
    CarrierFrequency::Float64
    MaximumDopplerShift::Int
    UTDirectionOfTravel::Vector{Float64}
    SampleRate::Int
    TransmitAntennaArray::Any
    TransmitArrayOrientation::Vector{Float64}
    ReceiveAntennaArray::Any
    ReceiveArrayOrientation::Vector{Float64}
    NormalizePathGains::Bool
    SampleDensity::Int
    InitialTime::Float64
    NumStrongestClusters::Int
    RandomStream::String
    Seed::Int
    NormalizeChannelOutputs::Bool
    ChannelFiltering::Bool
    NumTimeSamples::Int
    OutputDataType::String
    TransmitAndReceiveSwapped::Bool
end

# 初始化 cdl 对象
function initialize_cdl()
    cdl = CDLChannel(
        "Custom",
        [0, 4.7460e-08],
        [-82.2616, -92.1044],
        [165.9598, 165.8825],
        [-14.0400, -13.6505],
        [129.1157, 140.6526],
        [50.8847, 140.9978],
        true,
        13.3000,
        [11, 5, 3, 3],
        "Random",
        10.0,
        "Random",
        4.9000e+09,
        5,
        [0.0, 0.0],  # 这里假设 UTDirectionOfTravel 是 [0, 0]，实际需根据情况修改
        30720000,
        nothing,  # 这里暂时用 nothing 代替 phased.NRRectangularPanelArray，实际需实现相关类型
        [0.0, 0.0, 0.0],  # 假设 TransmitArrayOrientation 是 [0, 0, 0]，实际需根据情况修改
        nothing,  # 这里暂时用 nothing 代替 phased.NRRectangularPanelArray，实际需实现相关类型
        [0.0, 0.0, 0.0],  # 假设 ReceiveArrayOrientation 是 [0, 0, 0]，实际需根据情况修改
        true,
        64,
        0.0995,
        0,
        "mt19937ar with seed",
        73,
        true,
        false,
        15360,
        "single",
        true
    )
    return cdl
end

# 调用初始化函数
cdl = initialize_cdl()

# 打印初始化后的 cdl 对象
# println(cdl)

# 定义 clone 函数
function clone(cdl::CDLChannel)
    return CDLChannel(
        cdl.DelayProfile,
        copy(cdl.PathDelays),
        copy(cdl.AveragePathGains),
        copy(cdl.AnglesAoD),
        copy(cdl.AnglesAoA),
        copy(cdl.AnglesZoD),
        copy(cdl.AnglesZoA),
        cdl.HasLOSCluster,
        cdl.KFactorFirstCluster,
        copy(cdl.AngleSpreads),
        cdl.RayCoupling,
        cdl.XPR,
        cdl.InitialPhases,
        cdl.CarrierFrequency,
        cdl.MaximumDopplerShift,
        copy(cdl.UTDirectionOfTravel),
        cdl.SampleRate,
        cdl.TransmitAntennaArray,
        copy(cdl.TransmitArrayOrientation),
        cdl.ReceiveAntennaArray,
        copy(cdl.ReceiveArrayOrientation),
        cdl.NormalizePathGains,
        cdl.SampleDensity,
        cdl.InitialTime,
        cdl.NumStrongestClusters,
        cdl.RandomStream,
        cdl.Seed,
        cdl.NormalizeChannelOutputs,
        cdl.ChannelFiltering,
        cdl.NumTimeSamples,
        cdl.OutputDataType,
        cdl.TransmitAndReceiveSwapped
    )
end

# 定义 info 函数
function info(cdl::CDLChannel)
    return (
        PathDelays = cdl.PathDelays,
        AveragePathGains = cdl.AveragePathGains,
        AnglesAoD = cdl.AnglesAoD,
        AnglesAoA = cdl.AnglesAoA,
        AnglesZoD = cdl.AnglesZoD,
        AnglesZoA = cdl.AnglesZoA,
        KFactorFirstCluster = cdl.KFactorFirstCluster
    )
end

# 定义 wireless.internal.channelmodels.getCDLPerClusterParameters 函数
# 这里只是占位，需要根据实际情况实现
function getCDLPerClusterParameters(delay_profile::String)
    # 这里应该根据 delay_profile 返回实际的簇参数
    return (
        AngleSpread1 = [],
        AngleSpread2 = [],
        AngleSpread3 = [],
        AngleSpread4 = [],
        XPR = []
    )
end

function hMakeCustomCDL(cdl::CDLChannel)
    # Clone the input channel and reconfigure it for custom delay profile
    out = clone(cdl)
    out.DelayProfile = "Custom"

    # Get info from input channel
    cdlinfo = info(cdl)

    # Populate the delay profile from the info
    fields = [:PathDelays, :AveragePathGains, :AnglesAoD, :AnglesAoA, :AnglesZoD, :AnglesZoA]
    for f in fields
        setproperty!(out, f, getproperty(cdlinfo, f))
    end

    # Configure LOS cluster if required
    out.HasLOSCluster =!isinf(cdlinfo.KFactorFirstCluster)
    if out.HasLOSCluster
        # Configure K factor
        out.KFactorFirstCluster = cdlinfo.KFactorFirstCluster

        # For LOS channels, the first cluster is split into LOS and NLOS
        # parts in the info, so combine these parts into a single cluster
        p = out.AveragePathGains[1:2]
        out.AveragePathGains[2] = 10 * log10(sum(10 .^ (p / 10)))
        for f in fields
            deleteat!(getproperty(out, f), 1)
        end
    end

    # Configure AngleSpreads and XPR
    if lowercase(cdl.DelayProfile) != "custom"
        clusterParams = getCDLPerClusterParameters(cdl.DelayProfile)
        out.AngleSpreads = [clusterParams.AngleSpread1, clusterParams.AngleSpread2, clusterParams.AngleSpread3, clusterParams.AngleSpread4]
        out.XPR = clusterParams.XPR
    end

    return out
end
# println(out)